<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- 课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 一级类别 开始-->
              <ul class="clearfix">
                <li :class="{ current: $route.query.subjectParentId == null }">
                  <a
                    title="全部"
                    href="javascript:void(0);"
                    @click="searchSubjectLevelOne('')"
                    >全部</a
                  >
                </li>
                <!-- current: $route.query.subjectParentId == item.id vue 动态绑定属性如果当前的query.subjectParentId==item.id 则这个li的属性就是current -->
                <li
                  :class="{ current: $route.query.subjectParentId == item.id }"
                  v-for="item in subjectList"
                  :key="item.id"
                >
                  <!--searchSubjectLevelOne（）二级类别高亮显示  -->
                  <a
                    :title="item.title"
                    href="javascript:void(0);"
                    @click="searchSubjectLevelOne(item.id)"
                    >{{ item.title }}</a
                  >
                </li>
              </ul>
              <!-- /一级类别 结束-->
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14" />
            </dt>
            <dd class="c-s-dl-li">
              <!-- 二级类别 开始-->
              <ul class="clearfix">
                <li :class="{ current: $route.query.subjectId == null }">
                  <a
                    title="全部"
                    href="javascript:void(0);"
                    @click="subjectTwoLevel('')"
                    >全部</a
                  >
                </li>
                <li
                  v-for="(subjectTwo, index) in childrenList"
                  :key="index"
                  :class="{ current: $route.query.subjectId === subjectTwo.id }"
                >
                  <a
                    :title="subjectTwo.title"
                    href="javascript:void(0);"
                    @click="subjectTwoLevel(subjectTwo.id)"
                    >{{ subjectTwo.title }}</a
                  >
                </li>
              </ul>
              <!-- /二级类别 结束-->
            </dd>
          </dl>
          <div class="clear" />
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <!-- 排序 开始-->
            <ol class="js-tap clearfix">
              <li :class="{ 'current bg-green': searchObj.buyCountSort }">
                <a
                  title="销量"
                  href="javascript:void(0);"
                  @click="searchByCount()"
                  >销量
                  <i>↓</i>
                </a>
              </li>
              <li :class="{ 'current bg-green': searchObj.gmtCreateSort }">
                <a
                  title="最新"
                  href="javascript:void(0);"
                  @click="searchByLatest()"
                  >最新
                </a>
              </li>
              <li :class="{ 'current bg-green': searchObj.priceSort }">
                <a
                  title="价格"
                  href="javascript:void(0);"
                  @click="searchByPrice()"
                  >价格
                </a>
              </li>
            </ol>
            <!-- /排序 结束-->
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="courseList.length === 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam"
              >没有相关数据，小编正在努力整理中...</span
            >
          </section>
          <!-- /无数据提示 结束-->

          <!-- 数据列表 开始-->
          <article class="comm-course-list" v-if="courseList.length > 0">
            <ul id="bna" class="of">
              <li v-for="(course, index) in courseList" :key="index">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      alt="30天搞定Java核心技术"
                      :src="course.cover"
                      class="img-responsive"
                    />
                    <div class="cc-mask">
                      <a
                        :href="'/course/' + course.id"
                        title="开始学习"
                        class="comm-btn c-btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/' + course.id"
                      :title="course.title"
                      class="course-title fsize18 c-333"
                      >{{ course.title }}</a
                    >
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span
                      class="fr jgTag bg-green"
                      v-if="Number(course.price) === 0"
                    >
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span class="fr jgTag " v-else>
                      <i class="c-orange fsize12 f-fA">￥{{ course.price }}</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ course.viewCount }}人学习</i>
                      |
                      <i class="c-999 f-fA">{{ course.buyCount }}人购买</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear" />
          </article>
          <!-- /数据列表 结束-->
        </div>
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import { lightsteelblue } from "color-name";
import { Switch } from "element-ui";
import course from "../../api/course";
import subject from "../../api/subject";
export default {
  async asyncData({
    isDev,
    route,
    store,
    env,
    params,
    query,
    req,
    res,
    redirect,
    error,
  }) {
    let searchObj = {};
    //组装查询参数
    //组装url
    searchObj.subjectParentId = query.subjectParentId || "";
    searchObj.subjectId = query.subjectId || "";
    searchObj.priceSort = query.priceSort || "";
    searchObj.buyCountSort = query.buyCountSort || "";
    searchObj.gmtCreateSort = query.gmtCreateSort || "";

    //查询课程列表
    let resp = await course.getCourseList(searchObj);
    //查询课程分类
    let subjectResp = await subject.getSubjectList();

    let courseList = resp.data.courseList;
    let subjectList = subjectResp.data.items;

    let childrenList = [];
    //遍历一级分类找到二级分类
    subjectList.forEach((subject) => {
      if (subject.id == searchObj.subjectParentId) {
        childrenList = subject.children;
      }
    });
    return {
      courseList, //课程列表
      subjectList, //嵌套课程列表
      childrenList, //二级分类
      searchObj, //查询对象
    };
  },
  methods: {
    //传一级类别的id 找到二级类别
    searchSubjectLevelOne(subjectParentId) {
      window.location = "course?subjectParentId=" + subjectParentId;
    },
    //选择一级分类
    searchSubjectLevel(subjectParentId) {
      if (subjectParentId) {
        window.location = "course?subjectParentId=" + subjectParentId;
      } else {
        window.location = "course";
      }
    },
    //选择二级分类
    subjectTwoLevel(subjectId) {
      if (subjectId) {
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&subjectId=" +
          subjectId;
      } else {
        window.location =
          "course?subjectParentId=" + this.searchObj.subjectParentId;
      }
    },
    /**
     * 路由判断
     */
    //根据购买次数判断
    searchByCount() {
      if (!this.searchObj.subjectParentId && !this.searchObj.subjectId) {
        window.location = "course?buyCountSort=" + 1;
      }

      if (this.searchObj.subjectParentId && !this.searchObj.subjectId) {
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&buyCountSort=" +
          1;
      } else if (this.searchObj.subjectParentId && this.searchObj.subjectId)
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&subjectId=" +
          this.searchObj.subjectId +
          "&buyCountSort=" +
          1;
    },
    //根据时间判断
    searchByLatest() {
      if (!this.searchObj.subjectParentId && !this.searchObj.subjectId) {
        window.location = "course?gmtCreateSort=" + 1;
      }

      if (this.searchObj.subjectParentId && !this.searchObj.subjectId) {
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&gmtCreateSort=" +
          1;
      } else if (this.searchObj.subjectParentId && this.searchObj.subjectId)
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&subjectId=" +
          this.searchObj.subjectId +
          "&gmtCreateSort=" +
          1;
    },
    //根据价格判断
    searchByPrice() {
      if (!this.searchObj.subjectParentId && !this.searchObj.subjectId) {
        window.location = "course?priceSort=" + 1;
      }

      if (this.searchObj.subjectParentId && !this.searchObj.subjectId) {
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&priceSort=" +
          1;
      } else if (this.searchObj.subjectParentId && this.searchObj.subjectId)
        window.location =
          "course?subjectParentId=" +
          this.searchObj.subjectParentId +
          "&subjectId=" +
          this.searchObj.subjectId +
          "&priceSort=" +
          1;
    },
  },
};
</script>
